<?php
// +----------------------------------------------------------------------
// | Author: derek <693472824@qq.com> <http://www.bloveambition.com/>
// +----------------------------------------------------------------------

namespace Blovex4\Logic;

/**
 * 系统消息逻辑层
 *
 * @author derek <693472824@qq.com>
 */
class MessageLogic extends BaseLogic {
    
    /**
     * 保存站点信息
     * @param $workerID
     *          0：全部；1：自有团队；2：闲散小哥
     * @author derek <693472824@qq.com>
     */
    public function saveAction($content='', $workerID='0') {
        
        if(empty($content)){
            return false;
        }
        
        $available = AdminLogic::getSite();
        
        if(!empty($available) && is_numeric($available)){
            $data['site_id'] = $available;
        }else{
            $data['site_id'] = '0';
        }
        
        $data['worker_id'] = $workerID;
        $data['msg_type'] = '1';
        $data['order_id'] = '';
        $data['content'] = $content;
        $data['create_time'] = NOW_TIME;
        $data['sender'] = AdminLogic::is_login();
        $data['receipt'] = '1';
        $data['receipt_time'] = NOW_TIME;
        
        $msgModel = M('Message');
        $result = $msgModel->add($data);
        
        if($result){
            
            $extra['msg_type'] = '1';
            $extra['content'] = $content;
            $extra['order_id'] = '';
            $extra['order_no'] = '';
            $extra['emergency'] = '';
            $extra['appointment'] = '';
            $extra['delay'] = '';
            
            $siteStr = AdminLogic::getSite();
            if(!empty($siteStr)){
                
                $accoutList = M('Worker')->where(array('site_id'=>array('in', $siteStr)))->getField('account', true);
            }else{
                $accoutList = array();
            }
            
            $tag = $workerID == '0' ? array('1', '2') : array($workerID);
            
            api('Util/jPush', array($accoutList, $tag, $extra));
            
            return true;
        }else{
            return false;
        }
    }
}